package com.fingard.dsp.bank.directbank.gfgs01;

import java.math.BigDecimal;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/11/26.
 * 单笔余额、批量余额、历史余额
 */
public class GFGS01Balance extends GFGS01Base implements IBalance {
    /**
     * 查询余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception{
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try{
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                //拼接请求报文头
                GFGS01Header header=new GFGS01Header();
                header.billType="balquery";//交易码
                header.roottag="query";
                header.wsdlMethod="queryBalance";//提供webservice接口，查询实际、可用、历史余额信息
                writeCommonHeader(xmlTextWriter,header);
                //拼接请求报文体
                xmlTextWriter.writeStartElement("query");
                xmlTextWriter.writeElementString("acc",p_req7506.readedDetail.actNum);//帐号
                xmlTextWriter.writeElementString("querydate", Format.Date10Format.format(new Date()));//日期
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();
                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString(),header);

                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/ufinterface/sendresult/retcode");//返回码
                        String returnMsg = tmpBkReader.getFirstText("/ufinterface/sendresult/retmemo");//返回信息
                        if(isActiveRetCode(returnCode)){
                            detail.respCode = "0";//响应码
                            detail.respInfo = returnMsg;//响应信息
                            detail.actNum = tmpBkReader.getFirstText("/ufinterface/sendresult/acc");//账号
                            detail.actCur = getBankConfig().getMapKeyByValue("curActMapping",detail.actNum);//币种
                            double availBal=new BigDecimal(tmpBkReader.getFirstText("/ufinterface/sendresult/usebal"))
                                    .setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//可用余额
                            detail.availBal = availBal+"";//可用余额
                            double currBal=new BigDecimal(tmpBkReader.getFirstText("/ufinterface/sendresult/factbal"))
                                    .setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//余额
                            detail.currBal = currBal+"";//余额
                            detail.frozenBal="";//冻结余额
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = returnMsg;
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = tmpSb.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        }catch (Exception e){
            WriteBankLogLn("查询余额异常"+e);
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception{
        return null;
    }
}
